package com.platform.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

public class ImgUtils {

    /**
     *
     * @param pic1
     * @param x -5 ~ 5
     * @param y -5 ~ 5
     * @return
     */
    public static BufferedImage cutImg(BufferedImage pic1,int x,int y){
        if (x > 5) {
            x = 5;
        }
        if (x < -5) {
            x = -5;
        }
        if (y > 5) {
            y = 5;
        }
        if (y < -5) {
            y = -5;
        }
        int width = pic1.getWidth();
        int height = pic1.getHeight();
        int m  = (width/20);
        int n = (height /20);

        //参数依次为，截取起点的x坐标，y坐标，截取宽度，截取高度
        return pic1.getSubimage(width/4 + x * m, height/4 + y * n, width/2, height/2);
    }

    public static void main(String[] args) {
        String soursePicPath = "C:\\Users\\txw\\Desktop\\20200414224518681.png";
        File sourcePic = new File(soursePicPath);

        try{
            BufferedImage pic1 = ImageIO.read(sourcePic);
            BufferedImage pic2 = cutImg(pic1,0,0);
//            File desImage = new File("C:\\Users\\txw\\Desktop\\subjavaPic.png");
//            ImageIO.write(pic2, "png", desImage);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static ByteBuffer convertImageData(BufferedImage bi) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bi, "png", out);
            return ByteBuffer.wrap(out.toByteArray());
        } catch (IOException ex) {
            //TODO
        }
        return null;
    }
}
